package com.game.autoTool.project.web.common;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 * 截图后识别
 */
public class ScreenshotExample {
    public static void main(String[] args) {
        try {
            // 创建 Robot 对象
            Robot robot = new Robot();
            // 获取屏幕尺寸--截全屏
            /*Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            Rectangle screenRect = new Rectangle(screenSize);
            // 截取屏幕
            BufferedImage screenCapture = robot.createScreenCapture(screenRect);
*/
            // 指定截图的起始坐标和大小
            int x = 100; // 起始 x 坐标
            int y = 100; // 起始 y 坐标
            int width = 300; // 截图宽度
            int height = 200; // 截图高度

            Rectangle captureRect = new Rectangle(x, y, width, height);
            // 截取指定区域的屏幕
            BufferedImage screenCapture = robot.createScreenCapture(captureRect);

            // 获取项目目录
            String projectDir = System.getProperty("user.dir");
            File screenshotDir = new File(projectDir, "screenshot");
            if (!screenshotDir.exists()) {
                if (screenshotDir.mkdirs()) {
                    System.out.println("screenshot 目录创建成功");
                } else {
                    System.out.println("screenshot 目录创建失败");
                    return;
                }
            }

            // 保存截图为文件
            File outputFile = new File(screenshotDir, "screenshot.png");
            ImageIO.write(screenCapture, "png", outputFile);
            System.out.println("截图已保存为: " + outputFile.getAbsolutePath());

            String s = Ocr.imgParser(outputFile.getAbsolutePath());

            System.out.println(s);

        } catch (AWTException | IOException e) {
            e.printStackTrace();
        }
    }
}